package com.zy;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhaoyi
 * @date 2020/11/4 10:43
 */
public class UpdateDate {

    public static void main(String[] args) throws SQLException {


        List<Entity> result = Db.use("/pro")
                //.query("SELECT * FROM `insight_industry_result` WHERE analysis_date>='2020-01-01' AND analysis_date<= '2020-05-01' AND business_id IS NULL ");
                .query("SELECT * FROM `insight_industry_result` WHERE analysis_date>='2020-01-01' AND business_id IS NULL AND large_industry is not null ");
                //.query("SELECT * FROM `insight_industry_result` WHERE analysis_date='2020-05-01' AND business_id IS NULL AND large_industry is not null ");


        for (Entity entity : result) {

            String id = entity.getStr("id");
            String s_411 = entity.getStr("s_411");

            if (s_411 != null && s_411.length() > 0){
                s_411 = formatNm(s_411);
            }

            String s_412 = entity.getStr("s_412");
            if (s_412 != null && s_412.length() > 0){
                s_412 = formatNm(s_412);
            }


            String s411_415 = entity.getStr("s411_415");
            if (s411_415 != null && s411_415.length() > 0){
                s411_415 = formatNm(s411_415);
            }

            //s411_415

            Db.use("/pro").update(
                    Entity.create()
                            //.set("s_412", s_412)
                            //.set("s_411", s_411)
                            .set("s411_415",s411_415), //修改的数据
                    Entity.create("insight_industry_result")
                            .set("id", id)//where条件
                    /*Entity.create("insight_industry_result")
                            .set("analysis_date", entity.get("analysis_date"))
                            .set("large_industry", entity.getStr("large_industry"))
                            .set("industry", entity.getStr("industry"))*/
            );


            /*Db.use("/dev").insert(
                    Entity.create("insight_industry_result")
                            .set("large_industry",entity.getStr("large_industry"))
                            .set("industry",entity.getStr("industry"))
                            .set("analysis_date",entity.get("analysis_date"))
                            .set("s_411",s_411)
                            .set("s_412",s_412)
                            .set("s_413",entity.getStr("s_413"))
                            .set("s_414",entity.getStr("s_414"))
                            .set("s411_415",entity.getStr("s411_415"))
                            .set("s_415",entity.getStr("s_415"))
                            .set("s_421",entity.getLong("s_421"))
                            .set("s_422",entity.getStr("s_422"))
                            .set("s_431",entity.getStr("s_431"))
                            .set("s_432",entity.getStr("s_432"))
                            .set("s_433",entity.getStr("s_433"))
                            .set("s_434",entity.getStr("s_434"))
                            .set("s_441",entity.getStr("s_441"))
                            .set("s_442",entity.getStr("s_442"))
                            .set("status",entity.getStr("status"))
                            .set("msg",entity.getStr("msg"))
                            .set("s431_434",entity.getStr("s431_434"))
            );*/


        }


    }

    private static String formatNm(String context) {
        Pattern r = Pattern.compile("([1-9]\\d*\\.\\d*%|0\\.\\d*[1-9]\\d*%)");
        Matcher m = r.matcher(context);
        int i = 0;
        while (m.find()) {
            i++;
            String group = m.group();
            String value = group.replaceAll("%", "");
            DecimalFormat df = new DecimalFormat("0.00");
            String format = df.format(Double.valueOf(value));
            System.out.println("********* group is " + group + " format is  " + format + " *********");
            context = context.replaceAll(group, format + "%");
        }
        if (i > 1) {
            System.out.println("注意注意注意注意注意注意注意注意注意注意注意注意注意注意注意");
        }
        //System.out.println(context);
        //System.out.println("=======================");
        return context;
    }

}
